package com.tarena.lbs.es;


import com.alibaba.fastjson2.JSON;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.dos.StoreDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.GeoDistanceQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeanUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ESSearchStore {
    private RestHighLevelClient client;
    @Before
    public void initClient(){
        //构造一个可以连接本地localhost:9200的client对象
        HttpHost host=new HttpHost("localhost",9200,"http");
        RestClientBuilder builder = RestClient.builder(host);
        client=new RestHighLevelClient(builder);
    }
    @Test
    public void testStore() throws BusinessException {
        SearchRequest request=new SearchRequest("lbs_store");
        SearchSourceBuilder source=new SearchSourceBuilder();
        //2. 封装请求中的各种查询参数
        //2.1 只取一个值
//        source.from(0);
//        source.size(1);
        //2.2 地理位置的查询条件
        org.elasticsearch.common.geo.GeoPoint geoPoint=new org.elasticsearch.common.geo.GeoPoint(Double.valueOf("39.95933"),Double.valueOf("116.29845"));
        GeoDistanceQueryBuilder query = QueryBuilders.geoDistanceQuery("location");
        query.point(geoPoint).distance(10D, DistanceUnit.KILOMETERS);
        source.query(query);
        request.source(source);
        //3. 拿到响应
        try{
            SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
            log.info("一共查到{}条数据",searchResponse.getHits().getTotalHits().value);
            if(searchResponse.getHits().getTotalHits().value>0L){
                for (int i=0;i<searchResponse.getHits().getTotalHits().value;i++){
                    //可以封装返回 一个storeBO
                    SearchHit hit = searchResponse.getHits().getHits()[i];
                    //当初下入的json 将json读取转化会do 封装成bo返回
                    StoreDO storeDO = JSON.parseObject(hit.getSourceAsString(), StoreDO.class);
                    StoreBO bo=new StoreBO();
                    BeanUtils.copyProperties(storeDO,bo);
                    log.info("取到的store是:{}",bo);
                }
            }
        }catch (Exception e){
            log.error("查询范围内的店铺 失败",e);
            throw new BusinessException("-2","定位上报查询失败");
        }
    }
    @Test
    public void ESArticleTest() throws IOException {
        //1.根据文档id 更新文档部分数据
        UpdateRequest request=new UpdateRequest("lbs_content","4f883f13-5ca6-47dc-b361-390d0b3b1453");
        //2.携带更新的内容
        Map<String,Object> updateFileds=new HashMap<>();

        updateFileds.put("articleStatus","0");

        request.doc(updateFileds);
        //3.执行请求
        client.update(request,RequestOptions.DEFAULT);
    }
}
